package com.zbu.Object.toString;

import java.util.Objects;

public class Student {
    String name;
    int age;

    /*
     //自己写的
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Student) {
            //this -> 调用该方法的对象
            // obj -> stu2 (形参是父类 传进来的是子类 则不能直接调用成员变量，需要强转)
            Student stu = (Student) obj;
            return this.name.equals(stu.name) && this.age == stu.age;
        }
        else {
            return false;
        }
    }
     */
   //利用IDEA快捷键生成 重写的equals


    @Override
    public boolean equals(Object o) {
        //getClass 调用字节码文件  ;   getClass() != o.getClass() 比较两个字节码文件是否相同
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;

        /*
        public static boolean equals(Object a, Object b) {
            return (a == b) || (a != null && a.equals(b));
        }
         */
        //this 已省略
        return age == student.age && Objects.equals(name, student.name);
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
}
